package com.fitness.fitness.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fitness.fitness.convertor.EquipmentConvertor;
import com.fitness.fitness.modal.resource.dto.EquipmentDto;
import com.fitness.fitness.modal.resource.entity.Equipment;
import com.fitness.fitness.modal.resource.params.EquipmentPageQueryParams;
import com.fitness.fitness.modal.resource.vo.EquipmentVO;
import com.fitness.fitness.modal.response.ServerResponseEntity;
import com.fitness.fitness.service.IEquipmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/equipment")
@RequiredArgsConstructor
public class EquipmentController {

    private final IEquipmentService equipmentService;
    private final EquipmentConvertor equipmentConvertor;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    public ServerResponseEntity<IPage<EquipmentVO>> page(EquipmentPageQueryParams queryParams) {
        Page<EquipmentVO> result = equipmentService.getEqupmentPage(queryParams);
        return ServerResponseEntity.success(result);
    }

    /**
     * 新增
     * @param equipmentDto
     * @return
     */
    @PostMapping("/add")
    public ServerResponseEntity<Boolean> add(@RequestBody EquipmentDto equipmentDto) {
        String sn = equipmentDto.getSn();
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sn", sn);
        if(equipmentService.getOne(queryWrapper) != null) {
            return ServerResponseEntity.fail("E10001","该设备号已存在");
        }

        boolean result = equipmentService.addEquipment(equipmentDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("E10001","添加设备失败");
    }

    /**
     * 修改
     * @param equipmentDto
     * @return
     */
    @PutMapping("/update")
    public ServerResponseEntity<Boolean> update(@RequestBody EquipmentDto equipmentDto) {
        boolean result = equipmentService.updateEquipment(equipmentDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("E10002","更新设备信息失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public ServerResponseEntity<Boolean> delete(@RequestBody Long id) {
        boolean result = equipmentService.removeById(id);

        if(result) {
            return ServerResponseEntity.success(true);
        }
        return ServerResponseEntity.fail("C10003","删除设备失败");
    }

    /**
     * 获取设备表单
     */
    @GetMapping("/getEquipmentForm")
    public ServerResponseEntity<EquipmentVO> getEquipmentForm(Long id) {
        Equipment result = equipmentService.getById(id);
        return ServerResponseEntity.success(equipmentConvertor.entity2Vo(result));
    }
}
